import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**简易指令属性枚举*/
export const enum E_BriCommon {
	/**主类型*/
	TYPE = 0,
	/**id*/
	ID = 1,
	/**次类型*/
	CMD = 2,
	/**数字参数*/
	NUMV = 3,
	/**字符串参数*/
	STRV = 4,
	/**枚举数量*/
	EnumCount = 5
}


/**简易指令*/
export class BriCommon {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_BRI_COMMON_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_BRI_COMMON_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: BriCommon[] = [];

	/**创建入口*/
	public static create(json?: string): BriCommon {
		const c = this.POOL.pop() || new BriCommon();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**主类型 (uint8)*/
	protected _type: number;
	/**id (uint32)*/
	protected _id: number;
	/**次类型 (uint8)*/
	protected _cmd: number;
	/**数字参数 (long)*/
	protected _numv: number;
	/**字符串参数*/
	protected _strv: string;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (BriCommon.POOL.indexOf(this) === -1) {
			BriCommon.POOL.push(this);
		}
	}

	/**主类型*/
	public get type() {
		return this._type;
	}


	/**主类型*/
	public set type(v: number) {
		this._type = v;
	}


	/**id*/
	public get id() {
		return this._id;
	}


	/**id*/
	public set id(v: number) {
		this._id = v;
	}


	/**次类型*/
	public get cmd() {
		return this._cmd;
	}


	/**次类型*/
	public set cmd(v: number) {
		this._cmd = v;
	}


	/**数字参数*/
	public get numv() {
		return this._numv;
	}


	/**数字参数*/
	public set numv(v: number) {
		this._numv = v;
	}


	/**字符串参数*/
	public get strv() {
		return this._strv;
	}


	/**字符串参数*/
	public set strv(v: string) {
		this._strv = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_BriCommon): any {
		switch (k) {
			case E_BriCommon.TYPE: return this._type;
			case E_BriCommon.ID: return this._id;
			case E_BriCommon.CMD: return this._cmd;
			case E_BriCommon.NUMV: return this._numv;
			case E_BriCommon.STRV: return this._strv;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_BriCommon, v: any): void {
		switch (k) {
			case E_BriCommon.TYPE: this._type = v; break;
			case E_BriCommon.ID: this._id = v; break;
			case E_BriCommon.CMD: this._cmd = v; break;
			case E_BriCommon.NUMV: this._numv = v; break;
			case E_BriCommon.STRV: this._strv = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//主类型
		this._type = 0;
		//id
		this._id = 0;
		//次类型
		this._cmd = 0;
		//数字参数
		this._numv = 0;
		//字符串参数
		this._strv = '';
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._type) {
			json.type = this._type;
		}
		if (this._id) {
			json.id = this._id;
		}
		if (this._cmd) {
			json.cmd = this._cmd;
		}
		if (this._numv) {
			json.numv = this._numv;
		}
		if (this._strv) {
			json.strv = this._strv;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.type) {
			this._type = json.type;
		}
		if (json.id) {
			this._id = json.id;
		}
		if (json.cmd) {
			this._cmd = json.cmd;
		}
		if (json.numv) {
			this._numv = json.numv;
		}
		if (json.strv) {
			this._strv = json.strv;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint8(this._type);
		byte.writeUint32(this._id);
		byte.writeUint8(this._cmd);
		byte.writeLongNumber(this._numv);
		byte.writeString(this._strv);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._type = byte.readUint8();
		this._id = byte.readUint32();
		this._cmd = byte.readUint8();
		this._numv = byte.readLongNumber();
		this._strv = byte.readString();
		return this;
	}


	/**克隆*/
	public clone(): BriCommon {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = BriCommon.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = BriCommon.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//主类型
			if (idx === E_BriCommon.TYPE) {
				const oldv = this._type;
				this._type = us[i + 1];
				IAsync.dispatch(eventID, idx, this._type, oldv);
				i += 2;
				continue;
			}
			//id
			if (idx === E_BriCommon.ID) {
				const oldv = this._id;
				this._id = us[i + 1];
				IAsync.dispatch(eventID, idx, this._id, oldv);
				i += 2;
				continue;
			}
			//次类型
			if (idx === E_BriCommon.CMD) {
				const oldv = this._cmd;
				this._cmd = us[i + 1];
				IAsync.dispatch(eventID, idx, this._cmd, oldv);
				i += 2;
				continue;
			}
			//数字参数
			if (idx === E_BriCommon.NUMV) {
				const oldv = this._numv;
				this._numv = us[i + 1];
				IAsync.dispatch(eventID, idx, this._numv, oldv);
				i += 2;
				continue;
			}
			//字符串参数
			if (idx === E_BriCommon.STRV) {
				const oldv = this._strv;
				this._strv = us[i + 1];
				IAsync.dispatch(eventID, idx, this._strv, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in BriCommon with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: BriCommon): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._type !== other._type) {
			updates.push(E_BriCommon.TYPE, this._type);
		}
		if (this._id !== other._id) {
			updates.push(E_BriCommon.ID, this._id);
		}
		if (this._cmd !== other._cmd) {
			updates.push(E_BriCommon.CMD, this._cmd);
		}
		if (this._numv !== other._numv) {
			updates.push(E_BriCommon.NUMV, this._numv);
		}
		if (this._strv !== other._strv) {
			updates.push(E_BriCommon.STRV, this._strv);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(BriCommon.TOTAL_EVENT_ID, this.getV(0));
	}

}
